/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.Menu;
import com.jb.web.bean.MenuItem;
import com.jb.web.context.WebContext;

import com.jb.faces.component.UIComponent;


/**
 *
 */
public class PopupItemRenderer extends MenuItemRenderer {
    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        Menu mi = (Menu) itemToRender;

        return renderMenuItem(pCtx, mi);
    }

    protected String getDisplayName(WebContext pCtx, MenuItem pMenuItem) {
        if ("-".equals(pMenuItem.getValue()) && !pCtx.isRunTime()) {
            return "--";
        }

        String valueStr = (String) evaluate(pCtx, pMenuItem, pMenuItem.getValue());

        return isEmptyString(valueStr) ? ("MenuItem " + pMenuItem.getSequence()) : valueStr;
    }

    StringBuffer renderMenuItem(WebContext pCtx, Menu mi) {
        StringBuffer stringBuffer = new StringBuffer("");

        if (pCtx.isRunTime() || !mi.isDesignable()) {
            StringBuffer scriptBuffer = new StringBuffer("");

            scriptBuffer.append("\n<script>\n<!--\n");
            scriptBuffer.append(renderSubmenu(pCtx, mi, true));
            scriptBuffer.append("\n//--></script>\n");

            pCtx.addPostBodyScript(scriptBuffer.toString());
        } else {
            stringBuffer.append("<img ").append(getEditScript(pCtx, mi))
                        .append(addTagProperty("src", "resources/images/palette/JPopupMenu.gif", null)).append(" />");
        }

        return stringBuffer;
    }

    String renderSubmenu(WebContext pCtx, MenuItem mi, boolean bRoot) {
        long mid = 0;
        StringBuffer scriptBuffer = new StringBuffer("");
        String id = mi.getId();
        String width = "120";

        if (mi.getChildCount() > 0) {
            width = mi.getChild(0).getWidth();
        }

        if ((width == null) || (width.length() == 0)) {
            width = "120";
        }

        scriptBuffer.append("menu" + id + " = new JBMenu(" + id + ",'" + width + "',\"\"," + "\"" +
            mi.getRoot().getId() + "\"," + "\"" + mi.getBackgroundStyle() + mi.getBorderStyle() + "\"" + ");").append("\n");

        if (!bRoot) {
            scriptBuffer.append("menu" + ((Menu) mi.getParent()).getId() + ".items[\"menu" + id +
                "\"].setSubMenu(menu" + id + ")\n");
        }

        if (mi.getChildCount() == 0) {
            scriptBuffer.append("with (menu" + id + ") {").append("\n");

            String displayName = "add new item";
            long childId = mid++;
            scriptBuffer.append("addMenuItem(new menuItem(").append("\"" + displayName + "\",")
                        .append("\"" + "menu" + childId + "\",").append("\"\",").append("true,")
                        .append("'" + mi.getFontStyle() + mi.getBackgroundStyle() + mi.getBorderStyle() + "',")
                        .append("'" + mi.getHoverStyle() + "',").append("\"" + childId + "\",")
                        .append("\"" + mi.getRoot().getId() + "\"").append("));").append("\n");
            scriptBuffer.append("}");

            return scriptBuffer.toString();
        } else {
            scriptBuffer.append("\n").append("with (menu" + id + ") {").append("\n");

            for (int i = 0; i < mi.getChildCount(); i++) {
                MenuItem childNode = (MenuItem) mi.getChild(i);
                String action = (String) evaluate(pCtx, childNode, childNode.getDestination());
                String dest = pCtx.formURL(action, childNode);

                if (!pCtx.isRunTime()) {
                    dest = getSelectAction(pCtx, childNode);
                }

                String displayName = getDisplayName(pCtx, childNode);
                scriptBuffer.append("\n").append("addMenuItem(new menuItem(").append("\"" + displayName + "\",")
                            .append("\"" + "menu" + childNode.getId() + "\",").append("\"" + dest + "\",")
                            .append("true,")
                            .append("'" + childNode.getFontStyle() + childNode.getBackgroundStyle() +
                    childNode.getBorderStyle() + "',").append("'" + childNode.getHoverStyle() + "',")
                            .append("\"" + childNode.getId() + "\",").append("\"" + childNode.getRoot().getId() +
                    "\",")
                            .append("\"" + (isEmptyString(childNode.getIconImage()) ? "" : childNode.getIconImage()) +
                    "\"").append("));").append("\n");
            }

            scriptBuffer.append("}\n");

            for (int i = 0; i < mi.getChildCount(); i++) {
                MenuItem childNode = (MenuItem) mi.getChild(i);
                String displayName = getDisplayName(pCtx, childNode);

                if (((childNode.getChildCount() > 0) || !pCtx.isRunTime()) && !"--".equals(displayName)) {
                    scriptBuffer.append(renderSubmenu(pCtx, childNode, false));
                }
            }

            return scriptBuffer.toString();
        }
    }
}
